// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_gray");
    var top = require("../top/top_white");
    var nature = $("#nature").val();
	var enterpriseName;
	var businessLicence;
	var address;
	var contactAddress;
	var businessScope;
	var registeredCapital;
	var businessTimeLimit;
	var legalPerson;
	var legalIdcard;
	var legalPeriod;
	var depositBank;
	var accountHolder;
	var creditCard;
	var businessType;
	var enterpriseType;
	var	belongsEnterpriseCode;
	var proxySaleCode;
	var codeUSC;
	var certificate;
	var procurator;
	var procuratorIdcard;
	var procuratorPeriod;
	var flag = false;
	var province;
	var city;
	var district;
	var enterpriseId;
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
    
    var init = function(){
    	if(base.getData("sid")==null){
    		window.location="../login/login.html";
    	}
    	
    	enterpriseId = base.getUrlParam("enterpriseId");
    	top.init("企业资质认证");
    	footer.init();
    	$("#head").removeClass("none");
    	$(".login").addClass("none");
    	$("#main").removeClass("none");
    	$("#footer").removeClass("none");
    	router.init();
		initEvent(); 
    };
    
	var initEvent = function(){
		
		//调显示与隐藏函数
		hideSpan();
		hideCardType();
		$("#main").on("change", "#sUserType", function(){
			hideSpan();
		});
		
		$("#main").on("change", "#scardType", function(){
			hideCardType();
		});
		
		$("#main").on("change", ".businessTimeLimitCheck", function(){
			if ($("#main .businessTimeLimitCheck").prop("checked")) {
//				$("#businessTimeLimit").val(0);
				$("#businessTimeLimit").attr("disabled", "disabled").val(0);
			} else {
				$("#businessTimeLimit").removeAttr("disabled").val("");
			}
		});
		
		$("#main").on("change", "#creditCard", function(){
			if ($.trim($(this).val()) == "") {
				return;
			}
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				base.alert(2, "未知开户行！");
				$("#depositBank").removeAttr("disabled");
				$("#depositBank").removeAttr("readonly");
			} else {
				$("#depositBank").val(bn);
				$("#depositBank").attr("disabled", "disabled");
				$("#depositBank").attr("readonly", "readonly");
			}
		});
		
		$("#main").on("change", "#legalIdcard", function(){
			var promptString = validate.isIdCard($(this), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
			} 
		});
		
		layer.tips(tools.formatCurrency($("input[name=registeredCapital]").val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
		  tips: [1, '#3595CC'],
		  time: 3000
		});
		
		$("#main").on("focus", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$("#main").on("change", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$('.bankInfo').card({
            container: $('.card-wrapper'),
            formSelectors: {
            	numberInput: "input[name=creditCard]",
            	nameInput: 'input[name="accountHolder"]',
			    expiryInput: 'input[name="expiry"]',
			    cvcInput: 'input[name="cvc"]'
            }
        });
		
		$("#main").on("click", ".next", function(){
			var promptString = validate.isIdCard($("#legalIdcard"), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
				return;
			} 
			
			if(enterpriseId!=null){
				$("#main").attr("action","updateEnterprise");
				
			}else{
				$("#main").attr("action","createEnterprise");
			}
			databind.submitDom($("#main"), function(data){
				if(enterpriseId!=null){
	                if (data.status == 200) {
						router.route("approve/approve_upload.html?enterpriseId="+enterpriseId);
						addOrganize();
	                } else {
	                }
				}else{
	                if (data.status == 200) {
						router.route("approve/approve_upload.html?enterpriseId="+data.datas[0].enterpriseId);
						addOrganize();
	                } else {
	                }
	            }
		  	});
		});
		
//		单位类型 nature 
//		企业名 enterpriseName 
//		单位注册号 businessLicence 
//		营业执照所在地 address
//		联系地址 contactAddress
//		经营范围 businessScope
//		注册资金 registeredCapital
//		营业期限 businessTimeLimit
//		法定代表人姓名 legalPerson
//		身份证号 legalIdcard
//		证件有效期 legalPeriod
//		开户行 depositBank
//		开户名称 accountHolder
//		银行卡号 creditCard

		databind.fillInput1($("#main"), {action: "inquireCurrentEnterpriseDetail.json"}, function(data){
			if(data.datas[0].auditState == 2 || data.datas[0].auditState == 4) {
				$("#enterpriseName").attr("disabled", "disabled");
			}
			
			$("select[name=city]").html("<option>"+data.datas[0].city+"</option>");
			$("select[name=district]").html("<option>"+data.datas[0].district+"</option>");
			
			if (data.datas[0].authentication == 3 || data.datas[0].authentication == 2 || data.datas[0].authentication == 6 || data.datas[0].authentication == 7) {
				databind.disableInput($("#approve_main"));
				$(".next").remove();
				$("#progress").remove();
			}
			
			if ($("#businessTimeLimit").val() == "0") {
				$(".businessTimeLimitCheck").prop("checked", "checked");
				$("#businessTimeLimit").attr("disabled", "disabled");
			} else {
				$("#businessTimeLimit").removeAttr("disabled");
			}
			
			hideCardType();
			hideSpan();
		}, false);

    };
	
		//联动1
	var hideSpan = function() {
		var sUserType = $("#sUserType").val();
		if(sUserType == "以代理人身份注册") {
			$("#hide1").show();
			$("#hide2").show();
			$("#procurator").attr('isNull',1);
			$("#procuratorIdcard").attr('isNull',1);
			$("#procuratorPeriod").attr('isNull',1);
		}
		if(sUserType == "以法定代表人身份注册") {
			$("#hide2").hide();
			$("#hide1").show();
			$("#procurator").attr('isNull',0);
			$("#procuratorIdcard").attr('isNull',0);
			$("#procuratorPeriod").attr('isNull',0);
		}
	};
	
	//联动2
	var hideCardType = function() {
		var scardType = $("#scardType").val();
		if(scardType == "0") {
			$("#hideRegNo").show();
			$("#certificateShow").show();
			$("#codeUSCHide").hide();
			$("#codeUSC").attr('isNull',0);
			$("#regNo").attr('isNull',1);
			$("#certificate").attr('isNull',1);
		}
		if(scardType == "1") {
			$("#hideRegNo").hide();
			$("#certificateShow").hide();
			$("#codeUSCHide").show();
			$("#codeUSC").attr('isNull',1);
			$("#regNo").attr('isNull',0);
			$("#certificate").attr('isNull',0);
		}
	};
	
	//连接e签宝生成证书,返回账户唯一标识
    var addOrganize = function(){
		base.fnAjax({
			action: "addOrganize.json",
			async: false,
			oData: {
            }, //传递参数
            fnSuccess: function(data) {
            }
		});
	};
	
    return {
        init:init
    };
});